home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / nehe10.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  8.1 KB  |  251 lines

  1. ' Globals
  2.     
  3.     dim blend                       ' Blending ON/OFF
  4.     const piover180# = 0.0174532925
  5.     dim heading#
  6.     dim xpos#
  7.     dim zpos#
  8.  
  9.     dim yrot#                       ' Y Rotation
  10.     dim walkbias#
  11.     dim walkbiasangle#
  12.     dim lookupdown#
  13.     dim z#                          ' Depth Into The Screen
  14.  
  15.     dim filter                      ' Which Filter To Use
  16.     dim texture(2)                  ' Storage For 3 Textures
  17.  
  18. ' Data structures
  19.  
  20.     struc SVERTEX
  21.         dim x#, y#, z#
  22.         dim u#, v#
  23.     endstruc
  24.     
  25.     struc STRIANGLE
  26.         dim SVERTEX vertex(2)
  27.     endstruc
  28.     
  29.     struc SSECTOR
  30.         dim numtriangles
  31.         dim STRIANGLE &triangle()
  32.     endstruc
  33.     
  34.     dim SSECTOR sector1             ' Our Model Goes Here
  35.  
  36. ' Working variables
  37.     
  38.     dim file, oneline$, numtriangles, loop, vert, tempstr$, tempval#
  39.     dim _x#, _y#, _z#, _u#, _v#
  40.  
  41.     goto start
  42.     
  43. ' Routines
  44.  
  45. CheckError:
  46.     if FileError () <> "" then print FileError (): end endif
  47.     return
  48.  
  49. LoadLine:
  50.     ' Read a line
  51.     gosub CheckError
  52.     oneline$ = ReadLine (file)
  53.     
  54.     ' Skip comment line(s)
  55.     if len (oneline$) > 2 and left$ (oneline$, 2) = "//" then goto LoadLine endif
  56.     return
  57.     
  58. ScanFloat:
  59.     ' Skip whitespace
  60.     while oneline$ <> "" and left$ (oneline$, 1) <= " "
  61.         oneline$ = mid$ (oneline$, 2, 999)
  62.     wend
  63.     
  64.     ' Extract non whitespace
  65.     tempstr$ = ""
  66.     while oneline$ <> "" and left$ (oneline$, 1) > " "
  67.         tempstr$ = tempstr$ + left$ (oneline$, 1)
  68.         oneline$ = mid$ (oneline$, 2, 999)
  69.     wend
  70.     
  71.     ' Convert to floating point value
  72.     tempval# = val (tempstr$)
  73.     return
  74.  
  75. start:
  76.  
  77.     ' Load world file
  78.  
  79.     file = OpenFileRead ("files/world.txt")     ' File To Load World Data From
  80.     gosub CheckError
  81.     
  82.     gosub LoadLine
  83.     numtriangles = val (mid$ (oneline$, len ("NUMPOLLIES "), 999))
  84.     alloc sector1.triangle, numtriangles 
  85.     sector1.numtriangles = numtriangles
  86.     for loop = 0 to numtriangles - 1
  87.         for vert = 0 to 2
  88.             gosub LoadLine
  89.             gosub ScanFloat: _x# = tempVal#
  90.             gosub ScanFloat: _y# = tempVal#
  91.             gosub ScanFloat: _z# = tempVal#
  92.             gosub ScanFloat: _u# = tempVal#
  93.             gosub ScanFloat: _v# = tempVal#
  94.             sector1.triangle (loop).vertex (vert).x# = _x#
  95.             sector1.triangle (loop).vertex (vert).y# = _y#
  96.             sector1.triangle (loop).vertex (vert).z# = _z#
  97.             sector1.triangle (loop).vertex (vert).u# = _u#
  98.             sector1.triangle (loop).vertex (vert).v# = _v#
  99.         next
  100.     next
  101.     CloseFile (file)
  102.     
  103.     ' Load texture
  104.     
  105.     dim image
  106.     image = LoadImage ("Data/Mud.bmp")                  
  107.     glGenTextures (3, texture)
  108.     
  109.     glBindTexture (GL_TEXTURE_2D, texture (0))
  110.     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  111.     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  112.     glTexImage2D (GL_TEXTURE_2D, 0, 3, ImageWidth(image), ImageHeight(image), 0, ImageFormat(image), ImageDataType(image), image)
  113.     
  114.     glBindTexture (GL_TEXTURE_2D, texture (1))
  115.     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
  116.     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
  117.     glTexImage2D (GL_TEXTURE_2D, 0, 3, ImageWidth(image), ImageHeight(image), 0, ImageFormat(image), ImageDataType(image), image)
  118.     
  119.     glBindTexture (GL_TEXTURE_2D, texture (2))
  120.     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
  121.     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST)
  122.     gluBuild2DMipmaps (GL_TEXTURE_2D, 3, ImageWidth(image), ImageHeight(image), ImageFormat(image), ImageDataType(image), image)
  123.  
  124.     DeleteImage (image)
  125.  
  126.     ' Setup OpenGL
  127.  
  128.     glBlendFunc(GL_SRC_ALPHA,GL_ONE)                    ' Set The Blending Function For Translucency
  129.     glEnable (GL_TEXTURE_2D)
  130.     glMatrixMode (GL_PROJECTION)
  131.     glLoadIdentity ()
  132.     gluPerspective (45, WindowWidth () * 1.0 / WindowHeight (), 0.1, 100)
  133.     glMatrixMode (GL_MODELVIEW)
  134.     
  135.     ' Main loop    
  136.     
  137.     dim x_m#, y_m#, z_m#, u_m#, v_m#
  138.     dim xtrans#, ytrans#, ztrans#
  139.     dim sceneroty#
  140.     dim loop_m
  141.     dim key$
  142.     while true 
  143.     
  144.         ' Draw scene
  145.         glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)  ' Clear The Screen And The Depth Buffer
  146.         glLoadIdentity()                                     ' Reset The View
  147.         
  148.         xtrans# = -xpos#
  149.         ztrans# = -zpos#
  150.         ytrans# = -walkbias#-0.25
  151.         sceneroty# = 360.0 - yrot#
  152.  
  153.         glRotatef(lookupdown#,1.0,0,0)
  154.         glRotatef(sceneroty#,0,1.0,0)
  155.     
  156.         glTranslatef(xtrans#, ytrans#, ztrans#)
  157.         glBindTexture(GL_TEXTURE_2D, texture (filter))
  158.     
  159.         numtriangles = sector1.numtriangles
  160.     
  161.         ' Process Each Triangle
  162.  
  163.         for loop_m = 0 to numtriangles - 1
  164.  
  165.             glBegin(GL_TRIANGLES)
  166.                 glNormal3f( 0.0, 0.0, 1.0)
  167.                 x_m# = sector1.triangle(loop_m).vertex(0).x#
  168.                 y_m# = sector1.triangle(loop_m).vertex(0).y#
  169.                 z_m# = sector1.triangle(loop_m).vertex(0).z#
  170.                 u_m# = sector1.triangle(loop_m).vertex(0).u#
  171.                 v_m# = sector1.triangle(loop_m).vertex(0).v#
  172.                 glTexCoord2f(u_m#,v_m#): glVertex3f(x_m#,y_m#,z_m#)
  173.             
  174.                 x_m# = sector1.triangle(loop_m).vertex(1).x#
  175.                 y_m# = sector1.triangle(loop_m).vertex(1).y#
  176.                 z_m# = sector1.triangle(loop_m).vertex(1).z#
  177.                 u_m# = sector1.triangle(loop_m).vertex(1).u#
  178.                 v_m# = sector1.triangle(loop_m).vertex(1).v#
  179.                 glTexCoord2f(u_m#,v_m#): glVertex3f(x_m#,y_m#,z_m#)
  180.             
  181.                 x_m# = sector1.triangle(loop_m).vertex(2).x#
  182.                 y_m# = sector1.triangle(loop_m).vertex(2).y#
  183.                 z_m# = sector1.triangle(loop_m).vertex(2).z#
  184.                 u_m# = sector1.triangle(loop_m).vertex(2).u#
  185.                 v_m# = sector1.triangle(loop_m).vertex(2).v#
  186.                 glTexCoord2f(u_m#,v_m#): glVertex3f(x_m#,y_m#,z_m#)
  187.             glEnd()
  188.         next
  189.         SwapBuffers ()
  190.  
  191.         ' Move around
  192.         
  193.         key$ = inkey$ ()
  194.         if key$ = "B" or key$ = "b" then 
  195.             blend = not blend 
  196.             if not blend then
  197.                 glDisable (GL_BLEND)
  198.                 glEnable (GL_DEPTH_TEST)
  199.             else
  200.                 glEnable (GL_BLEND)
  201.                 glDisable (GL_DEPTH_TEST)
  202.             endif
  203.         endif
  204.         if key$ = "F" or key$ = "f" then
  205.             filter = filter + 1
  206.             if filter > 2 then 
  207.                 filter = 0 
  208.             endif
  209.         endif
  210.         if ScanKeyDown (VK_PRIOR) then
  211.             z# = z# - 0.02
  212.         endif
  213.         if ScanKeyDown (VK_NEXT) then
  214.             z# = z# + 0.02
  215.         endif
  216.         if ScanKeyDown (VK_UP) then
  217.             xpos# = xpos# - sin (heading# * piover180#) * 0.05
  218.             zpos# = zpos# - cos (heading# * piover180#) * 0.05
  219.             if walkbiasangle# >= 359 then
  220.                 walkbiasangle# = 0
  221.             else
  222.                 walkbiasangle# = walkbiasangle# + 10
  223.             endif
  224.             walkbias# = sin(walkbiasangle# * piover180#) / 20
  225.         endif
  226.         if ScanKeyDown (VK_DOWN) then
  227.             xpos# = xpos# + sin (heading# * piover180#) * 0.05
  228.             zpos# = zpos# + cos (heading# * piover180#) * 0.05
  229.             if walkbiasangle# <= 1 then
  230.                 walkbiasangle# = 359
  231.             else
  232.                 walkbiasangle# = walkbiasangle# - 10
  233.             endif
  234.             walkbias# = sin(walkbiasangle# * piover180#) / 20
  235.         endif
  236.         if ScanKeyDown (VK_RIGHT) then
  237.             heading# = heading# - 1
  238.             yrot# = heading#
  239.         endif
  240.         if ScanKeyDown (VK_LEFT) then
  241.             heading# = heading# + 1
  242.             yrot# = heading#
  243.         endif
  244.  
  245.         if ScanKeyDown (VK_PRIOR) then
  246.             lookupdown# = lookupdown# - 1
  247.         endif
  248.         if ScanKeyDown (VK_NEXT) then
  249.             lookupdown# = lookupdown# + 1
  250.         endif
  251.     wend